/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.constant.Constant;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Mdict;
import com.thinkgem.jeesite.modules.sys.service.MdictService;

/**
 * 多级字典表Controller
 * @author zad
 * @version 2017-10-17
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/mdict")
public class MdictController extends BaseController {

	@Autowired
	private MdictService mdictService;
	
	@ModelAttribute
	public Mdict get(@RequestParam(required=false) String id) {
		Mdict entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mdictService.get(id);
		}
		if (entity == null){
			entity = new Mdict();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:mdict:view")
	@RequestMapping(value = {"list", ""})
	public String list(Mdict mdict, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Mdict> page = mdictService.findPage(new Page<Mdict>(request, response), mdict); 
		model.addAttribute("page", page);
		return "modules/sys/mdictList";
	}

	@RequiresPermissions("sys:mdict:view")
	@RequestMapping(value = "form")
	public String form(Mdict mdict, Model model) {
		model.addAttribute("mdict", mdict);
		
		
		
		List<Mdict> mdicts = this.mdictService.findList(new Mdict());
		model.addAttribute("mdicts",mdicts);
		
		return "modules/sys/mdictForm";
	}

	@RequiresPermissions("sys:mdict:edit")
	@RequestMapping(value = "save")
	public String save(Mdict mdict, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, mdict)){
			return form(mdict, model);
		}
		
		
		mdictService.save(mdict);
		addMessage(redirectAttributes, "保存字典成功");
		return "redirect:"+Global.getAdminPath()+"/sys/mdict/?repage";
	}
	
	@RequiresPermissions("sys:mdict:edit")
	@RequestMapping(value = "delete")
	public String delete(Mdict mdict, RedirectAttributes redirectAttributes) {
		mdictService.delete(mdict);
		addMessage(redirectAttributes, "删除字典成功");
		return "redirect:"+Global.getAdminPath()+"/sys/mdict/?repage";
	}

}